<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Shopping extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function __construct() {
        parent::__construct();
        $this->load->model('m_hoa');
        $this->load->model('m_hoadon');
        $this->load->model('m_cthd');
    }

    function index() {
        $this->load->view('cart');
    }

    function add() {
        $hoa = $this->m_hoa->getProduct($this->input->post('id'));
        $item = array(
            'id' => $this->input->post('id'),
            'qty' => 1,
            'price' => $hoa->DonGia,
            'name' => $hoa->TenHoa,
            'options' => array('MaLoaiHoa' => $hoa->MaLoaiHoa, 'MaMauSac' => $hoa->MaMauSac, 'HinhAnh' => $hoa->HinhAnh)
        );
        if ($this->cart->insert($item) == true) {
            $this->load->view('cart');
        }
    }

    function remove($rowid) {
        $item = array(
            'rowid' => $rowid,
            'qty' => 0
        );
        $this->cart->update($item);
        redirect('/shopping', 'refresh');
    }

    function update($rowid, $qty) {
        $item = array(
            'rowid' => $rowid,
            'qty' => $qty
        );
        $this->cart->update($item);
        redirect('/shopping', 'refresh');
    }

    function destroy() {
        if ($this->cart->destroy()) {
            echo 'Destroyed';
        }
    }

    function check_out() {
        $str = date("Y-m-d H:i:s");
        $gencode = str_replace(':', null, str_replace(' ', null, str_replace('-', null, $str)));
        $MaHoaDon = $gencode;

        if ($this->input->post('id_user') != null) {
            $MaNguoiDung = $this->input->post('id_user');
        } else {
            $MaNguoiDung = 0;
        }
        $order = array(
            'MaHoaDon' => $MaHoaDon,
            'MaNguoiDung' => $MaNguoiDung,
            'EmailNguoiDung' => $this->input->post('email_sender'),
            'TenNguoiGui' => $this->input->post('name_sender'),
            'SDTNguoiGui' => $this->input->post('phone_sender'),
            'DiaChiNguoiGui' => $this->input->post('address_sender'),
            'TenNguoiNhan' => $this->input->post('name_receiver'),
            'SDTNguoiNhan' => $this->input->post('phone_receiver'),
            'DiaChiNguoiNhan' => $this->input->post('address_receiver'),
            'NgayGiao' => date('Y-m-d H:i:s', strtotime($this->input->post('date_receive'))),
            'TinhTrang' => 'Đang Chờ',
        );
        if ($this->m_hoadon->insertOrder($order)) {
            if ($this->insert_Order_Detail($MaHoaDon) == true) {
                $this->destroy();
                $data['msg'] = "Quý Khách Đã Đặt Hàng Thành Công";
                $data['pg'] = "category";
                $this->load->view('info_success', $data);
            }
        } else {
            $data['msg'] = "Có Lỗi Trong Quá Trình Đặt Hàng,Quý Khách Vui Lòng Kiểm Tra Lại Thông Tin";
            $this->load->view('info_error', $data);
        }
    }

    function insert_Order_Detail($MaHoaDon) {
        $ck = true;
        $cart = $this->cart->contents();
        foreach ($cart as $value) {
            $order_detail = array(
                'MaHoaDon' => $MaHoaDon,
                'MaHoa' => $value['id'],
                'SoLuong' => $this->input->post('SoLuong'),
                'DonGia' => $value['price'],
                'ThanhTien' => $value['price']*$this->input->post('SoLuong'),
            );
            if ($this->m_hoa->updateSoLuong($value['id'],$this->m_hoa->getSoLuong($value['id'])-$this->input->post('SoLuong')) == false) {
                $ck = false;
                break;
            }
            if ($this->m_cthd->insertOrderDetail($order_detail) == false) {
                $ck = false;
                break;
            }
        }
        return $ck;
    }

}
